//
//  JGDrupTableHandler.m
//  JGExpandTableDemo2
//
//  Created by mac on 15/9/18.
//  Copyright © 2015年 Free. All rights reserved.
//

#import "JGDrupTableHandler.h"
#import "JGDrupCellFactory.h"
#import "JGDrupSectionViewCell.h"
#import "JGDrupDetailViewCell.h"

@interface JGDrupTableHandler ()<UITableViewDataSource,UITableViewDelegate>
{
    //记录当前选中的行
    NSIndexPath *_indexPath;
    
    NSDictionary *_insertDic;
}

- (void)seletedIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView;

@end

@implementation JGDrupTableHandler

- (id)init
{
    self = [super init];
    if (self)
    {
         _contentArray = [NSMutableArray array];
    }
    
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _contentArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [JGDrupCellFactory tableView:tableView withInfo:self.contentArray[indexPath.row]];
    //未展开状态的cell
    if ([cell isMemberOfClass:[JGDrupSectionViewCell class]])
    {
        //红点的控制
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [JGDrupCellFactory tableViewWithInfo:self.contentArray[indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.controller hidePicker]; //没有用到
    
#warning mark 打开任意一个实现不同的效果
     //此方法判断关闭当前的cell,打开点击对应的cell过程
//    [self closeCurrentCellWithIndexPath:indexPath andTableView:tableView];
    
    //此方法只是点开展开对应的cell，点击其他cell时不会自动关闭
    [self seletedIndexPath:indexPath inTableView:tableView];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.controller hidePicker];
}

/**
 *  私有方法
 */
- (void)p_inserRowData:(NSArray *)array withIndextPath:(NSIndexPath *)indexPath withTableView:(UITableView *)tableView
{
    _insertDic = @{@"type":@"2",@"subClothes":array};
   // NSLog(@"%ld",indexPath.row + 1);
    [self.contentArray insertObject:_insertDic atIndex:indexPath.row + 1];
    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    [tableView endUpdates];
}

- (void)p_delRowDataIndextPath:(NSIndexPath *)indexPath withTableView:(UITableView *)tableView
{
   // NSLog(@"%ld",indexPath.row + 1);
    [self.contentArray removeObjectAtIndex:indexPath.row + 1];
    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];
}

- (void)seletedIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
{
    NSDictionary *infoDic = [self.contentArray objectOrNilAtIndex:indexPath.row];
    NSMutableDictionary *dic = [infoDic mutableCopy];
    if ([[infoDic objectOrNilForKey:@"type"] isEqualToString:@"1"])
    {
        //不展开直接跳转
        JGDrupSectionViewCell *cell = (JGDrupSectionViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        NSString *string = infoDic[@"title"];
        if ([string isEqualToString:@"要跳转的标题名称"]) {
            //跳转到对应的界面
            
            return;
        }
        
        if (cell.isOpen == NO) {
            //展开
            [(JGDrupViewController *)tableView.viewcontroller loadData];
            [self p_inserRowData:self.contentArray[indexPath.row][@"subClothes"] withIndextPath:indexPath withTableView:tableView];
            
            [dic setObject:@(YES) forKey:@"isOpen"];
            
        } else {
            //关闭
            [self p_delRowDataIndextPath:indexPath withTableView:tableView];
            
            [dic setObject:@(NO) forKey:@"isOpen"];
        }
        
        //设置好dic的isOpen属性
        [self.contentArray replaceObjectAtIndex:indexPath.row withObject:dic];
        cell.isOpen = !cell.isOpen;
        
    }
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


/**
 * 判断关闭当前的打开点击的过程
 */
- (void)closeCurrentCellWithIndexPath:(NSIndexPath *)indexPath andTableView:(UITableView *)tableView
{
    //NSLog(@"%@---%@",indexPath,_indexPath);
    if (_indexPath &&  _indexPath != indexPath) {
        indexPath = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0];
    }
    //NSLog(@"%@",indexPath);
    
    if (_indexPath == nil) {
        [self seletedIndexPath:indexPath inTableView:tableView];
        _indexPath = indexPath;
    } else if (_indexPath == indexPath) {
        //第一次点击或者与之前选中的行一样
        [self seletedIndexPath:indexPath inTableView:tableView];
        _indexPath = nil;
    } else {
        //收回之前展开的子cell的数据后，展开当前选中的cell
        [self removeOriginSubCellDic:tableView];
        __weak typeof(self) weakSelf = self;
        [weakSelf seletedIndexPath:indexPath inTableView:tableView];
        _indexPath = indexPath;
    }
}

/**
 * 删除当前展开的cell
 */
- (void) removeOriginSubCellDic:(UITableView *)tableView{
    
    NSUInteger index = [self.contentArray indexOfObject:_insertDic];
   // NSLog(@"%lu",(unsigned long)index);
   // NSLog(@"%@",_insertDic);
    
    NSMutableDictionary *dic = [[self.contentArray objectAtIndex:index - 1] mutableCopy];
    [dic setObject:@(NO) forKey:@"isOpen"];
    [self.contentArray replaceObjectAtIndex:index - 1 withObject:dic];
    
   // NSLog(@"%@",[self.contentArray objectAtIndex:index - 1]);
    
    [self.contentArray removeObject:_insertDic];
    [tableView reloadData];
}



@end
